<?php
namespace Portal\Controller;
use Common\Controller\HomebaseController;
class HGridController extends HomebaseController {
	function index(){
		$this->ajaxReturn(sp_ajax_return('siccess','success',1));
	}
	
  function test(){
    $url = "./data/upload/20161223/585c9e09905e9.png";
    $image = new \Think\Image(); 
    $image->open($url);
    var_dump($image->width());
    var_dump(32*32);
  }
  
  function gridkind(){
//  $kind = M("Constant")->where("parentid = 3")->order("listorder desc")->limit(2)->select();
//  $indx = 0;
//  foreach($kind as $k){
//    $list[$indx]['name'] = $k['pvalue'];
//    $res =  M("Constant")->where("parentid = ".$k['id'])->order("listorder desc")->limit(8)->select();
//    $list[$indx]['data'] =$res;
//    $indx++;
//  }
    $res = M("Publish")->alias("a")->field("a.*,b.icon,c.pvalue,d.nick,d.icon as headimg")->
        join("left join m_img_lib as b on b.publish_id = a.id")->
        join("m_constant as c on c.id = a.type")->
        join("m_app_user as d on d.user_uniq_id = a.user_uniq_id")->
        where("a.status = 3")->order("a.create_time desc,b.is_first desc")->limit(5)->group("a.id")->select();
    $img = M("ImgLib")->where("publish_id is not null")->select();
    for($i=0,$len=count($res);$i<$len;++$i){
      $res[$i]['create_time'] = formatTime($res[$i]['create_time']);
    }
    for($k=0,$klen=count($res);$k<$klen;++$k){
      for($j=0,$jlen=count($img);$j<$jlen;++$j){
        if($img[$j]['publish_id']==$res[$k]['id']){
          $cnt = count($res[$k]['img']);
          if($cnt==9)continue;
          $res[$k]['img'][$cnt] =$img[$j]['icon'];
        }
      }  
    }
    $this->res = $res;
    $this->version = $this->js_version();
//  $this->list = $list;
    $this->display();
  }

  function moregridkind(){
    $page = i("page");
    $type = i("type");
    $height = i("height");
    if(!empty($type)&&$type!="*"){
      $res = M("Publish")->alias("a")->field("a.*,b.icon,c.pvalue,d.nick,d.icon as headimg")->
        join("left join m_img_lib as b on b.publish_id = a.id")->
        join("m_constant as c on c.id = a.type")->
        join("m_app_user as d on d.user_uniq_id = a.user_uniq_id")->
        where("a.status = 3 and a.type = $type")->order("a.create_time desc,b.is_first desc")->page($page,5)->group("a.id")->select();
    }else{
      $res = M("Publish")->alias("a")->field("a.*,b.icon,c.pvalue,d.nick,d.icon as headimg")->
        join("left join m_img_lib as b on b.publish_id = a.id")->
        join("m_constant as c on c.id = a.type")->
        join("m_app_user as d on d.user_uniq_id = a.user_uniq_id")->
        where("a.status = 3")->order("a.create_time desc,b.is_first desc")->page($page,5)->group("a.id")->select();      
    }
    $img = M("ImgLib")->where("publish_id is not null")->select();
    for($k=0,$klen=count($res);$k<$klen;++$k){
      for($j=0,$jlen=count($img);$j<$jlen;++$j){
        if($img[$j]['publish_id']==$res[$k]['id']){
          $cnt = count($res[$k]['img']);
          if($cnt==9)continue;
          $res[$k]['img'][$cnt] =$img[$j]['icon'];
        }
      }  
    }
    foreach($res as $r){
      $img = "";
      foreach($r['img'] as $v){
        $img .= '<img class="lazy lazypage'.$page.'" onclick="imglibclick(this)" data-original="'.$v.'" height="'.$height.'"/>';  
      }
      $html .='<li>
          <div class="left-cell">
            <img src="'.$r["icon"].'"/>
          </div>
          <div class="right-cell">
            <div class="title">'.$r["nick"].'</div>
            <div class="content">
              <a href="/HGrid/kindmerchant/id/'.$r["id"].'">
              <div>'.mb_substr($r["content"], 0,114,"utf-8").'</div>
              </a>
              <div class="imglib">'.$img.'</div>
              <div class="date">
                '.formatTime($r["create_time"]).'
              </div>
            </div>
          </div>
        </li>';
    }
    $this->success($html);
  }
  
	function grid(){
		$user=sp_get_current_user();
//		if($user==false){
//			redirect("/HUser/login");
//		}
		$sql = "select a.*,c.icon,a.id as st_id,b.* from".
		" ( m_stand as a left join m_ad as b on a.id = b.stand_id )left join m_img_lib as c on ".
		" c.ad_id = b.id  GROUP BY st_id";
		$stand = M()->query($sql);
		$now = date('Y-m-d');
		for($i=0,$len = count($stand);$i<$len;++$i){
			if($stand[$i]['expire_date']<$now)$stand[$i]['ad_id']=null;
		}
		$error= session("griderror");
		if(isset($error))$this->error = $error;
		session("griderror",null);
		$this->stand = $stand;
    $this->hidead = $this->hide_ad();
    $this->version = $this->js_version();
		$this->display();
	}
	function merchant(){
		$id = $_GET['id'];
		$mer = M("Ad")->field("a.*,b.icon,b.is_first,c.pvalue as type")->alias("a")->join("left join m_img_lib as b on b.ad_id = a.id")->
		  join("left join m_constant as c on c.id = a.type_id")->where("a.id = $id")->select();
		$this->mer = $mer;
    $this->version = $this->js_version();
		$this->display();
	}
	
  function kindmerchant(){
    $id = i("id",null);
    if(empty($id))$this->error("参数错误");
    $mer = M("Publish")->field("a.*,b.icon,b.is_first")->alias("a")->
      join("left join m_img_lib as b on b.publish_id = a.id")->where("a.id = $id")->select();
    $typeid = empty($mer[0]['type'])?0:$mer[0]['type'];
    $type = M("Constant")->field("pvalue")->where("id = $typeid")->select();
    $this->type = $type[0]['pvalue'];
    $this->mer = $mer;
    $this->version = $this->js_version();
    $this->display();
  }
  function gridkindlist(){
//  $type = i("type",null);
//  if(!empty($type)){
//    $res = M("Publish")->alias("a")->field("a.*,b.icon,c.pvalue")->join("left join m_img_lib as b on b.publish_id = a.id ")->
//      join("m_constant as c on c.id = a.type")->where("a.type = $type and a.status = 3")->order("a.id desc,b.is_first desc")->select();
//  }else{
//    $res = M("Publish")->alias("a")->field("a.*,b.icon,c.pvalue")->join("left join m_img_lib as b on b.publish_id = a.id")->
//      join("m_constant as c on c.id = a.type")->where("a.status = 3")->order("a.id desc,b.is_first desc")->group("a.id")->select();
//  }
//  for($i=0,$len=count($res);$i<$len;++$i){
//    $res[$i]['create_time'] = formatTime($res[$i]['create_time']);
//  }
    $type = i("type",null);
    $city = i("city",null);
    if(isset($city)){
      if(strpos($city, "_")>=0){
        $sql_city=" and a.city_id = ".substr($city, 1);
        $join_city = " m_constant as e on e.parentid = a.city_id";
      }
      else{
         $join_city ="";
        $sql_city=" and a.city_id = $city";
      }
    } 
    if(!empty($type)&&$type!="*"){
      $kind = M("Constant")->where("parentid = 3 and id = $type")->select();
      $res = M("Publish")->alias("a")->field("a.*,b.icon,c.pvalue,d.nick,d.icon as headimg")->
        join("left join m_img_lib as b on b.publish_id = a.id")->
        join("m_constant as c on c.id = a.type")->
        join("m_app_user as d on d.user_uniq_id = a.user_uniq_id")->join($join_city)->
        where("a.status = 3 and a.type = $type".$sql_city)->order("a.create_time desc,b.is_first desc")->limit(5)->group("a.id")->select();   
    }else{
      $res = M("Publish")->alias("a")->field("a.*,b.icon,c.pvalue,d.nick,d.icon as headimg")->
        join("left join m_img_lib as b on b.publish_id = a.id")->
        join("m_constant as c on c.id = a.type")->
        join("m_app_user as d on d.user_uniq_id = a.user_uniq_id")->join($join_city)->
        where("a.status = 3".$sql_city)->order("a.create_time desc,b.is_first desc")->limit(5)->group("a.id")->select();      
    }
    $img = M("ImgLib")->where("publish_id is not null")->select();
    for($i=0,$len=count($res);$i<$len;++$i){
      $res[$i]['create_time'] = formatTime($res[$i]['create_time']);
    }
    for($k=0,$klen=count($res);$k<$klen;++$k){
      for($j=0,$jlen=count($img);$j<$jlen;++$j){
        if($img[$j]['publish_id']==$res[$k]['id']){
          $cnt = count($res[$k]['img']);
          if($cnt==9)continue;
          $res[$k]['img'][$cnt] =$img[$j]['icon'];
        }
      }  
    }
    $this->version = $this->js_version();
    $this->res = $res;
    $this->kind = $kind[0];
    $this->display();
  }
  
	function gridorder(){
		$user=sp_get_current_user();
//		if($user==false){
//			redirect("/HUser/login");
//		}
		$modify=session("modify");
		$const = M("Constant")->where("parentid = 21")->order("listorder")->select();
		$id = $_GET['order'];
		$order = M("Stand")->where("id = $id")->select();
		$ad = M()->query("select a.*,b.is_first,b.icon from m_ad as a left join m_img_lib as b on b.ad_id = a.id where a.stand_id = $id and a.user_uniq_id = '".$user['user_uniq_id']."'");
		$this->discount = $const;
		$this->order = $order[0];
		$this->modify = $modify;
		$this->user = $user;
		$this->id = $id;
		if($modify==1){
			$this->adnum = count($ad);
			$this->ad = $ad;
		}else{
			$this->adnum = 0;
		}
    $this->version = $this->js_version();
		session("modify",null);
    $this->hidead = $this->hide_ad();
		$this->display();
	}
	
	function kindorder(){
    $user=sp_get_current_user();
    $modify=session("kindmodify");
    $const = M("Constant")->where("parentid = 3")->select();
    $province = M("Constant")->where("parentid = 2")->select();
    if($modify==1){
      $id = $_GET['order'];
      $order = M("Publish")->alias("a")->field("a.*,b.pvalue")->join("m_constant as b on b.id = a.type")->
        where("a.id = $id")->select();
      $pic = M("ImgLib")->where("ad_id = $id")->select();
      $this->order = $order[0];
      $this->adnum = count($pic);
      $this->ad = $pic;
    }else{
      $this->adnum = 0;
    }
    $this->id=$id;
//  session("kindmodify",null);
    $this->province=$province;
    $this->kind= $const;
    $this->modify = $modify;
    $this->user = $user;
    $this->version = $this->js_version();
    $this->display();
  }
	
	function gridmodify(){
		$imgs =M("Ad")->field("a.*,b.is_first,b.icon")->alias("a")->join("left join m_img_lib as b on b.ad_id = a.id")->where("a.user_uniq_id = '".$data['user_uniq_id']."'")->select();
		for($i =0,$len = count($imgs);$i<$len;++$i){
			$data[$i] = $imgs[$i]['icon'];
			if($imgs[$i]['is_first']==2)$data[0]=$imgs[$i]['icon'];
		}
		session('modifyimgs',$data);
		session("modify",1);
		$id = $_GET['id'];
		redirect("/HGrid/gridorder/order/$id");
	}
  
  function kinddmodify(){
    $imgs =M("Publish")->field("a.*,b.is_first,b.icon")->alias("a")->
      join("left join m_img_lib as b on b.publish_id = a.id")->
      where("a.user_uniq_id = '".$data['user_uniq_id']."'")->select();
    for($i =0,$len = count($imgs);$i<$len;++$i){
      $data[$i] = $imgs[$i]['icon'];
      if($imgs[$i]['is_first']==2)$data[0]=$imgs[$i]['icon'];
    }
    session('kindmodifyimgs',$data);
    session("kindmodify",1);
    $id = $_GET['id'];
    redirect("/HGrid/kindorder/order/$id");
  }

}